package cn.lizemin.demo.thread.threadPool02;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

/**
 * @author: lizemin
 * @version: 1.0
 */
public class MyTest04 {
    //这是一个main方法,是程序的入口
    public static void main(String[] args) {
//        test1();
        test2();

    }
    private static void test1(){
        //1.使用工厂类获取线程池对象
        ExecutorService es = Executors.newSingleThreadExecutor();
        //2.提交任务
        for (int i = 0; i < 10; i++) {
            es.submit(new MyRunnable4(i));
        }
        //3.关闭线程池，仅仅是不再接受新的任务，以前的任务还是会继续执行
//        es.shutdown();
//        es.submit(new MyRunnable4(888));//不能再提交新的任务了

    }

    private static void test2(){
        //1.使用工厂类获取线程池对象
        ExecutorService es = Executors.newSingleThreadExecutor(new ThreadFactory(){
            int n;
            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r,"自定义的线程的名称-"+n++);
            }
        });

        //2.提交任务
        for (int i = 0; i < 10; i++) {
            es.submit(new MyRunnable4(i));
        }
        //3.立刻关闭线程池，如果线程池中还有缓存的任务没有执行，则取消执行，并返回这些
        List<Runnable> list = es.shutdownNow();
        for (Runnable runnable : list) {
            System.out.println(runnable);
        }

    }
}

class MyRunnable4 implements Runnable{

    private int id ; //

    public MyRunnable4(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        //获取线程的名称，打印一句话
        String name = Thread.currentThread().getName();
        System.out.println(name+"执行了任务"+id);

    }

    @Override
    public String toString() {
        return "MyRunnable4{" +
                "id=" + id +
                '}';
    }
}


